package com.lanxin.oa.login.interseptor;

import com.lanxin.bean.QxFunction;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class LoginInterseptor extends HandlerInterceptorAdapter{

	@Override
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
		System.out.println("拦截器*****");
		String url=request.getRequestURI();
		System.out.println(url+"*******");
		String webname=request.getContextPath();
		if(!url.contains("login") && !url.contains("view")){
			String empname=(String) request.getSession().getAttribute("empname");
			String zhanghao=(String) request.getSession().getAttribute("zhanghao");
			if(empname!=null && !empname.equals("")){
				if(zhanghao.equals("admin")){
					return true;
				}
				List path=(List) request.getSession().getAttribute("path");
				List<QxFunction> pathAll=(List<QxFunction>) request.getSession().getAttribute("pathAll");
				String endAll=null;
				String endurl=null;
				boolean s=false;
				if(pathAll!=null && pathAll.size()>0){
					for(int i=0;i<pathAll.size();i++){
						QxFunction all=(QxFunction) pathAll.get(i);
						endAll=webname+"/"+all.getClasses()+"/"+all.getMethod();
						if(url.equals(endAll)){
							s=true;
							break;
						}
					}
				}
				if(s==true){
					for(int j=0;j<path.size();j++){
						String str=(String) path.get(j);
						endurl=webname+"/"+str;
						if(url.equals(endurl)){
							s=false;
							break;
						}

					}
				}

				if(s==true){
					response.sendRedirect(webname+"/view/cuowu.jsp");
					return false;
				}

			}
			else
			{

				response.sendRedirect(webname+"/view/login/login.jsp");
				return false;
			}

		}
		return true;
	}



}

